package com.work.Controller.login;

import java.sql.*;

class Article
{
    int id;
    String title;
    String content;
    Timestamp timestamp;
}
public class ArticleDao {
     public  int insert(Article article)
     {
         String url="jdbc:mysql://localhost:3306/jspexam";
         Connection connection=null;
         try {
             Class.forName("com.mysql.jdbc.Driver");
              connection= DriverManager.getConnection(url,"root","root");
         } catch (Exception e) {
             e.printStackTrace();
         }
         try {
             PreparedStatement statement=connection.prepareStatement("insert into article values(?,?,?,?)");
             statement.setInt(1,article.id);
             statement.setString(2,article.title);
             statement.setString(3,article.content);
             statement.setTimestamp(4,article.timestamp);
             statement.execute();
             return 1;
         } catch (SQLException throwables) {
             throwables.printStackTrace();
             return 0;
         }

     }
     public Article findById(int id)
     {
         String url="jdbc:mysql://localhost:3306/jspexam";
         Connection connection=null;
         try {
             Class.forName("com.mysql.jdbc.Driver");
             connection= DriverManager.getConnection(url,"root","root");
         } catch (Exception e) {
             e.printStackTrace();
         }
         try {
             PreparedStatement statement=connection.prepareStatement("select * from article where id=?");
             statement.setInt(1,id);
             ResultSet rs=statement.executeQuery();
             if(rs.next())
             {
                 Article a=new Article();
                 a.id=rs.getInt(1);
                 a.title=rs.getString(2);
                 a.content=rs.getString(3);
                 a.timestamp=rs.getTimestamp(4);
                 return a;
             }
             return null;
         } catch (SQLException throwables) {
             throwables.printStackTrace();
             return null;
         }
     }
}
